/**
 * Created by Elsa on 2017/8/28.
 */
layui.define(['form', 'layedit', 'laydate','jquery'], function(exports){
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer,
        layedit = layui.layedit,
        laydate = layui.laydate;

    //自定义验证规则
    form.verify({
        title: function(value) {
            if(value.length < 5) {
                return '标题至少得5个字符啊';
            }
        },
        pass: [/(.+){6,12}$/, '密码必须6到12位'],
        content: function(value) {
            layedit.sync(editIndex);
        }
    });

    function toDecimal2(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(x*100)/100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    }

    //监听提交
    form.on('submit(caseglyx)', function(data) {
        $(this).attr('disabled','disabled');
        var OperaQualityTable = new Object();
        OperaQualityTable.rgstnum = $('input[name="rgstnum"]').val();
        OperaQualityTable.apnttime = data.field.apnttime;
        OperaQualityTable.test1 =  $('input[name="test1"]').is(':checked');
        OperaQualityTable.test2d1 =  $('input[name="test2d1"]').is(':checked');
        OperaQualityTable.test2d2 =  $('input[name="test2d2"]').is(':checked');
        OperaQualityTable.test2d3 =  $('input[name="test2d3"]').is(':checked');
        OperaQualityTable.test2d4 =  $('input[name="test2d4"]').is(':checked');
        OperaQualityTable.test2d5 =  $('input[name="test2d5"]').is(':checked');
        OperaQualityTable.test2d6 =  $('input[name="test2d6"]').is(':checked');
        OperaQualityTable.test2d7 =  $('input[name="test2d7"]').is(':checked');
        OperaQualityTable.test2d8 =  $('input[name="test2d8"]').is(':checked');
        OperaQualityTable.test2d9 =  $('input[name="test2d9"]').is(':checked');
        OperaQualityTable.test2d10 =  $('input[name="test2d10"]').is(':checked');
        OperaQualityTable.test3 = data.field.test3;
        OperaQualityTable.qccname = data.field.qccname;

        console.log(JSON.stringify(OperaQualityTable));

        $.ajax({
            type:"POST",
            url:contextPath+"/project/ops/qcc/new",
            contentType: "application/json; charset=utf-8",
            data:JSON.stringify(OperaQualityTable),
            dataType:"json",
            success:function (data) {
                console.log(data.message);
                if(data.message == "质量控制表创建成功!"){
                    layer.msg('质量控制表创建成功!<br>页面将在1秒后刷新...');
                    setTimeout(function(){window.location.reload();},1000);
                } else {
                    $('#suresubmitx').attr('disabled',false);
                    layer.msg(data.message, {
                        time:false,
                        btn:"确定",
                        btnAlign: 'c'
                    });
                }
            },
            error:function (e) {
            }
        });
        return false;
    });

    exports('OperaQualityTable', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});